/**
 * @desc uni 模态框方法封装, 包括 alert 和 confirm,内容可在项目初始化时自定义
 * @author 钱炜亮
 * @date 2024-07-19
 */

/**
 * @desc 显示 alert 弹窗
 * @param content 弹窗内容
 * @param title 弹窗标题
 * @param options 弹窗其他参数
 * @returns {Promise<unknown>}
 */
function alert(content = '', title = '提示', options = {}) {
	const baseOptions = {
		showCancel: false,
		confirmText: '确定'
	};
	return new Promise((resolve, reject) => {
		uni.showModal({
			title,
			content,
			...baseOptions,
			...options,
			success: (res) => {
				// 点击了确认
				if (res.confirm) {
					resolve(true);
				}
				// 点击了取消
				if (res.cancel) {
					reject(false);
				}
			}
		});
	});
}

/**
 * @desc 显示 confirm 弹窗
 * @param content 弹窗内容
 * @param title 弹窗标题
 * @param options 弹窗其他参数
 * @returns {Promise<unknown>}
 */
function confirm(content = '', title = '提示', options = {}) {
	const baseOptions = {
		showCancel: true,
		confirmText: '确定',
		cancelText: '取消'
	};
	return new Promise((resolve, reject) => {
		uni.showModal({
			title,
			content,
			...baseOptions,
			...options,
			success: (res) => {
				console.log(res);
				// 点击了确认
				if (res.confirm) {
					resolve(true);
				}
				// 点击了取消
				if (res.cancel) {
					reject(false);
				}
			}
		});
	});
}

export function initModal() {
	uni.$alert = alert;
	uni.$confirm = confirm;
}
